package org.origin.ferret.system.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.origin.centre.model.base.XPage;
import org.origin.centre.model.dto.XDeleteByIdDto;
import org.origin.centre.model.dto.XQueryByIdDto;
import org.origin.centre.model.enums.result.RS;
import org.origin.centre.model.result.R;
import org.origin.centre.model.vo.XPageVo;
import org.origin.ferret.system.model.dto.client.SysClientInsertDto;
import org.origin.ferret.system.model.dto.client.SysClientPageDto;
import org.origin.ferret.system.model.dto.client.SysClientPasswordDto;
import org.origin.ferret.system.model.dto.client.SysClientUpdateDto;
import org.origin.ferret.system.model.vo.client.SysClientVo;
import org.origin.ferret.system.service.SysClientService;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * 系统渠道-接口
 *
 * @author ferret
 * @version 2024-05-08
 */
@RestController
@Tag(name = "系统渠道-接口")
@RequiredArgsConstructor
@RequestMapping("${origin.api:}/sys/client")
@SuppressWarnings({"ELValidationInJSP", "SpringElInspection"})
public class SysClientController {
    private final SysClientService clientService;

    @PostMapping("page")
    @Operation(summary = "1.分页数据", description = "分页数据")
    @PreAuthorize("principal.hasAnyAuthority('sys:client:page')")
    public R<XPageVo<SysClientVo>> page(@Valid @RequestBody XPage<SysClientPageDto> pageDto) {
        return R.ok(RS.OK, this.clientService.page(pageDto));
    }

    @GetMapping("query")
    @Operation(summary = "2.查询数据", description = "查询数据")
    @PreAuthorize("principal.hasAnyAuthority('sys:client:query')")
    public R<SysClientVo> query(@Valid @ParameterObject XQueryByIdDto queryDto) {
        return R.ok(RS.OK, this.clientService.query(queryDto));
    }

    @PutMapping("insert")
    @Operation(summary = "3.插入数据", description = "插入数据")
    @PreAuthorize("principal.hasAnyAuthority('sys:client:insert')")
    public R<Boolean> insert(@Valid @RequestBody SysClientInsertDto insertDto) {
        return R.ok(RS.OK, this.clientService.insert(insertDto));
    }

    @PostMapping("update")
    @Operation(summary = "4.更新数据", description = "更新数据")
    @PreAuthorize("principal.hasAnyAuthority('sys:client:update')")
    public R<Boolean> update(@Valid @RequestBody SysClientUpdateDto updateDto) {
        return R.ok(RS.OK, this.clientService.update(updateDto));
    }

    @DeleteMapping("delete")
    @Operation(summary = "5.删除数据", description = "删除数据")
    @PreAuthorize("principal.hasAnyAuthority('sys:client:delete')")
    public R<Boolean> delete(@Valid @ParameterObject XDeleteByIdDto deleteDto) {
        return R.ok(RS.OK, this.clientService.delete(deleteDto));
    }

    @DeleteMapping("password")
    @Operation(summary = "6.修改密码", description = "修改密码")
    @PreAuthorize("principal.hasAnyAuthority('sys:client:password')")
    public R<Boolean> password(@Valid @ParameterObject SysClientPasswordDto passwordDto) {
        return R.ok(RS.OK, this.clientService.password(passwordDto));
    }

}
